
#include <Formatter.h>

Formatter::Formatter(const Amount & amount, std::ostream& out)
	:amount_m(amount), out_m(out)
{
}

void Formatter::formatStringInAllUnit()
{	
	amount_m.getNormalizedAmount()==0 ? formatZeroString() : formatNonZeroStringInAllUnit();
}

void Formatter::formatStringInBaseUnit()
{	
	amount_m.getNormalizedAmount()==0 ? formatZeroString() : formatNonZeroStringInBaseUnit();
}

void Formatter::formatZeroString()
{
	out_m << 0 << " " << amount_m.getBaseUnit();
}

void Formatter::formatNonZeroStringInAllUnit()
{
	firstUnitFound_m = false;
	amount_m.metric_m.printOrderedUnitsList(*this);
}

void Formatter::formatNonZeroStringInBaseUnit()
{
	firstUnitFound_m = false;
	amount_m.metric_m.printBaseUnitList(*this);
}

void Formatter::operator()(const Unit* unit)
{
	static int amountInBaseUnit_m = amount_m.getNormalizedAmount();
	unit->getFloorUnitStream(out_m, amountInBaseUnit_m, firstUnitFound_m);
	amountInBaseUnit_m = unit->getRestAmountInBaseUnit(amountInBaseUnit_m);
}
